<template>
  <a-spin class="w-[100%]" :size="35" :loading="spinLoading" tip="正在处理, 请稍候...">
    <a-form :model="form" ref="formRef" :rules="rules" auto-label-width>
      <div class="flex justify-between" style="flex-wrap: wrap">
        <a-form-item class="w-[100%]" field="plateNumberName" label="车牌号" required>
          <a-input v-model="form.plateNumberName" placeholder="请输入车牌号" />
        </a-form-item>
        <a-form-item class="w-[100%]" field="vehicleModel" label="车型名称">
          <a-input v-model="form.vehicleModel" placeholder="请输入车型名称" />
        </a-form-item>
        <a-form-item class="w-[100%]" field="fileUrl" label="文件URL">
          <a-textarea
            v-model="form.fileUrl"
            placeholder="请输入文件URL"
            allow-clear
            :auto-size="{ minRows: 3, maxRows: 5 }"
          />
        </a-form-item>
        <a-form-item class="w-[100%]" field="identifyResult" label="识别结果">
          <a-textarea
            v-model="form.identifyResult"
            placeholder="请输入识别结果"
            allow-clear
            :auto-size="{ minRows: 3, maxRows: 5 }"
          />
        </a-form-item>
      </div>
    </a-form>

    <!-- 分割线 -->
    <a-divider class="mt-0" />

    <div class="flex justify-end">
      <a-space>
        <a-button @click="cancelBtnClick()">取消</a-button>
        <a-button type="primary" @click="okBtnClick()">确定</a-button>
      </a-space>
    </div>
  </a-spin>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, watch } from "vue";
import { addApi, updateApi, getApi } from "~/api/tc/plateNumber.js";

//全局实例
const { proxy } = getCurrentInstance();
//接收父组件参数
const props = defineProps({
  params: {
    type: Object,
    default: () => {},
  },
});
//父组件函数
const emits = defineEmits(["ok", "cancel"]);
//加载中...
const spinLoading = ref(false);
//表单ref
const formRef = ref(null);
//表单
const form = reactive({
  //车牌ID
  plateNumberId: null,
  //车牌号
  plateNumberName: null,
  //车型名称
  vehicleModel: null,
  //文件ID
  fileId: null,
  //文件URL
  fileUrl: null,
  //识别结果
  identifyResult: null,
});

// 表单验证规则
const rules = {
  plateNumberName: [{ required: true, message: "请输入车牌号" }],
};

//确定 -> 点击
const okBtnClick = () => {
  //表单验证
  formRef.value.validate((valid) => {
    if (valid) {
      return false;
    }
    //添加
    if (props.params.operationType === proxy.operationType.add.type) {
      spinLoading.value = true;
      addApi(form)
        .then(() => {
          proxy.$msg.success(proxy.operationType.add.success);
          emits("ok");
        })
        .finally(() => {
          spinLoading.value = false;
        });
    }
    //修改
    if (props.params.operationType === proxy.operationType.update.type) {
      spinLoading.value = true;
      updateApi(form)
        .then(() => {
          proxy.$msg.success(proxy.operationType.update.success);
          emits("ok");
        })
        .finally(() => {
          spinLoading.value = false;
        });
    }
  });
};
//取消 -> 点击
const cancelBtnClick = () => {
  emits("cancel");
};

//加载车牌详细信息
const loadPlateNumberInfo = (plateNumberId) => {
  spinLoading.value = true;
  getApi(plateNumberId)
    .then((res) => {
      //数据赋值
      if (res) {
        for (let key in res) {
          if (form.hasOwnProperty(key)) {
            form[key] = res[key];
          }
        }
      }
    })
    .finally(() => {
      spinLoading.value = false;
    });
};
//监听参数变化
watch(
  () => props.params,
  (newVal, oldVal) => {
    //车牌ID
    if (props.params.plateNumberId) {
      //加载车牌信息
      loadPlateNumberInfo(props.params.plateNumberId);
    }
  },
  { deep: true, immediate: true }
);
</script>
<style scoped></style>
